package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zjx on 2018
 */

public class CreateZhaoPinActivity extends AppCompatActivity {

    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.fabiao_bt)
    RelativeLayout fabiaoBt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.editzhiwei_iv)
    ImageView editzhiweiIv;
    @BindView(R.id.editzhiwei_tv)
    TextView editzhiweiTv;
    @BindView(R.id.jobname_edit)
    EditText jobnameEdit;
    @BindView(R.id.editzhiwei_bt)
    RelativeLayout editzhiweiBt;
    @BindView(R.id.xueli_iv)
    ImageView xueliIv;
    @BindView(R.id.xueli_tv)
    TextView xueliTv;
    @BindView(R.id.jobeducation_edit)
    EditText jobeducationEdit;
    @BindView(R.id.xueli_bt)
    RelativeLayout xueliBt;
    @BindView(R.id.exprience_iv)
    ImageView exprienceIv;
    @BindView(R.id.exprience_tv)
    TextView exprienceTv;
    @BindView(R.id.jobname_experience)
    EditText jobnameExperience;
    @BindView(R.id.exprience_bt)
    RelativeLayout exprienceBt;
    @BindView(R.id.peoplenum_iv)
    ImageView peoplenumIv;
    @BindView(R.id.peoplenum_tv)
    TextView peoplenumTv;
    @BindView(R.id.peoplenum_edit)
    EditText peoplenumEdit;
    @BindView(R.id.peoeplenum_bt)
    RelativeLayout peoeplenumBt;
    @BindView(R.id.editmoney_iv)
    ImageView editmoneyIv;
    @BindView(R.id.editmoney_tv)
    TextView editmoneyTv;
    @BindView(R.id.jobsalary_edit)
    EditText jobsalaryEdit;
    @BindView(R.id.editmoney_bt)
    RelativeLayout editmoneyBt;
    @BindView(R.id.editcompany_iv)
    ImageView editcompanyIv;
    @BindView(R.id.editcompany_tv)
    TextView editcompanyTv;
    @BindView(R.id.jobcompany_edit)
    EditText jobcompanyEdit;
    @BindView(R.id.editcompany_bt)
    RelativeLayout editcompanyBt;
    @BindView(R.id.editlocation_iv)
    ImageView editlocationIv;
    @BindView(R.id.editlocation_tv)
    TextView editlocationTv;
    @BindView(R.id.content_city)
    TextView contentCity;
    @BindView(R.id.selectcity_bt)
    RelativeLayout selectcityBt;
    @BindView(R.id.editaddress_iv)
    ImageView editaddressIv;
    @BindView(R.id.editaddress_tv)
    TextView editaddressTv;
    @BindView(R.id.detailaddress_edit)
    EditText detailaddressEdit;
    @BindView(R.id.editaddress_bt)
    RelativeLayout editaddressBt;
    @BindView(R.id.editzhiyeinfo_iv)
    ImageView editzhiyeinfoIv;
    @BindView(R.id.editzhiyeinfo_tv)
    TextView editzhiyeinfoTv;
    @BindView(R.id.editzhiyeinfo_bt)
    RelativeLayout editzhiyeinfoBt;
    @BindView(R.id.jobdescription_edit)
    EditText jobdescriptionEdit;
    public static final int REQUEST_MAP = 103;
    private String address = "";
    private String area = "";
    private String city = "";
    private String province = "";
    public Dialog loadingdialog = null;
    private String mCurrentLat = "";
    private String mCurrentLon = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createzhaopin);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        this.loadingdialog = new Dialog(this);
        this.loadingdialog.setContentView(R.layout.dialog_loading);
        this.loadingdialog.setCanceledOnTouchOutside(false);
        this.loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @OnClick({R.id.backbt,R.id.fabiao_bt,R.id.selectcity_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.selectcity_bt:
                startActivityForResult(new Intent(this,LocationMapActivity.class),REQUEST_MAP);
                break;
            case R.id.backbt:
                finish();
                break;
            case R.id.fabiao_bt:
                if (TextUtils.isEmpty(this.jobnameEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未填写招聘岗位", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.jobeducationEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未填写学历要求", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.jobnameExperience.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未填写工作经验", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.jobsalaryEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未填写工资", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.jobcompanyEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未填写公司名称", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.contentCity.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未选择地点", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.detailaddressEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未选择地点", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.jobdescriptionEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "未描述招聘岗位的详细信息", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.peoplenumEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "人数不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                create(this.jobnameEdit.getText().toString().trim(), this.jobeducationEdit.getText().toString().trim(), this.jobnameExperience.getText().toString().trim(), this.jobsalaryEdit.getText().toString().trim(), this.jobcompanyEdit.getText().toString().trim(), this.province, this.city, this.area, this.detailaddressEdit.getText().toString(), this.mCurrentLon, this.mCurrentLat, this.jobdescriptionEdit.getText().toString());
                break;


        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
               if(resultCode!=RESULT_OK){
                  return;
               }
               switch (requestCode){
                   case REQUEST_MAP:
                       if(data==null){
                           return;
                       }
                       this.mCurrentLat = data.getStringExtra("mCurrentLat");
                       this.mCurrentLon = data.getStringExtra("mCurrentLon");
                       this.province = data.getStringExtra("province");
                       this.city = data.getStringExtra("city");
                       this.area = data.getStringExtra("area");
                       this.address = data.getStringExtra("address");
                       this.contentCity.setText("" + this.province + this.city + this.area);
                       this.detailaddressEdit.setText("" + this.address);
                       break;
               }

    }



    public void create(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6, String paramString7, String paramString8, String paramString9, String paramString10, String paramString11, String paramString12)
    {
        this.loadingdialog.show();
        HashMap localHashMap = new HashMap();
        List localList = Xutils.queryAll(getApplication(), User.class);
        localHashMap.put("merchantsId", "" + ((User)localList.get(0)).getMerchantsId());
        localHashMap.put("jobName", "" + paramString1);
        localHashMap.put("jobEducation", "" + paramString2);
        localHashMap.put("jobExperience", "" + paramString3);
        localHashMap.put("jobSalary", "" + paramString4);
        localHashMap.put("companyName", "" + paramString5);
        localHashMap.put("companyProvince", "" + paramString6);
        localHashMap.put("companyCity", "" + paramString7);
        localHashMap.put("companyArea", "" + paramString8);
        localHashMap.put("companyAddress", "" + paramString9);
        localHashMap.put("companyLongitude", "" + paramString10);
        localHashMap.put("companyLatitude", "" + paramString11);
        localHashMap.put("jobDescription", "" + paramString12);
        localHashMap.put("jobPeoplenum", this.peoplenumEdit.getText().toString().trim());
        Xutils.POST(HttpUrl.jobmerchantsadd, localHashMap, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                CreateZhaoPinActivity.this.loadingdialog.dismiss();
                try {
                    JSONObject jsonObject=new JSONObject(result);
                switch (jsonObject.getInt("state")){
                    case HttpCode.SUCCESS:
                        finish();
                        break;
                }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                CreateZhaoPinActivity.this.loadingdialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
